<template>
	<div class="child2">
		<h3>子组件2</h3>
		<h4>玩具:{{ toy }}</h4>
		<h4>child1给的玩具:{{ toy0 }}</h4>
	</div>
</template>

<script setup lang="ts" name="Child2">
import { onUnmounted, ref } from 'vue'
import emitter from '@/utils/emitter';
let toy = ref('游戏机')
let toy0 = ref('')

emitter.on('send-toy', (value: any) => {
	console.log('send-toy', value)
	toy0.value = value
})

// 在组件卸载时解绑事件
onUnmounted(() => {
	emitter.off('send-toy')
})
</script>

<style scoped>
.child2 {
	margin-top: 50px;
	background-color: orange;
	padding: 10px;
	box-shadow: 0 0 10px black;
	border-radius: 10px;
}
</style>